﻿var _validation = (function () {
    ko.extenders.required = function (target, overrideMessage) {
        target.hasError = ko.observable();
        target.validationMessage = ko.observable();

        function validate(newValue) {
            target.hasError(whoever.Validation.IsEmpty(newValue));
            target.validationMessage($.trim(newValue) ? "" : overrideMessage || "This field is required");
        }
        validate(target());
        target.subscribe(validate);

        return target;
    };

    ko.extenders.email = function (target, check) {
        target.hasError = ko.observable();

        function validateEmail(emailAddress) {
            target.hasError(!whoever.Validation.EmailIsValid(emailAddress));
        };
        validateEmail(target());
        target.subscribe(validateEmail);

        return target;
    };

    ko.extenders.emailRequired = function (target, check) {
        target.hasError = ko.observable();

        function validateEmail(emailAddress) {
            target.hasError(!whoever.Validation.EmailIsValid(emailAddress) || whoever.Validation.IsEmpty(emailAddress));
        };
        validateEmail(target());
        target.subscribe(validateEmail);

        return target;
    };

    ko.extenders.accountOrEmail = function (target, check) {
        target.hasError = ko.observable();

        function validate(value) {
            if (value)
                if (value.indexOf('@') !== -1)
                    target.hasError(!whoever.Validation.EmailIsValid(value));
                else
                    target.hasError(whoever.Validation.IsEmpty(value));
            else
                target.hasError(true);
        }
        validate(target());
        target.subscribe(validate);

        return target;
    };

    ko.extenders.regExpValidate = function (target, params) {
        target.validationMessage = ko.observable();
        if (params.mask() && params.mask() !== "") {
            target.hasError = ko.observable(true);
        }
        else {
            target.hasError = ko.observable(false);
        }

        validate(target());

        target.subscribe(validate);

        return target;

        function validate(newValue) {
            if (params.mask() && params.mask() !== "") {
                var regExp = new RegExp(params.mask());
                target.hasError(regExp.test(newValue) ? false : true);
            }
            else {
                target.hasError(false);
            }
        }
    };

    return {
        ValidateByHTML5: function (element, value) {
            var result = false;

            if ($(element).attr("required"))
                if (_.isEmpty(value))
                    result = true;

            if ($(element).attr("type")) {
                var type = $(element).attr("type");

                if (type == "numeric")
                    if (value)
                        if (!$.isNumeric(value))
                            result = true;

                if (type == "email")
                    if (value)
                        if (!whoever.Validation.EmailIsValid(value))
                            result = true;
            }

            if ($(element).attr("pattern")) {
                if (value) {
                    var regExp = new RegExp($(element).attr("pattern"));
                    result = regExp.test(value) ? false : true;
                }
            }

            return result;
        },
        ModelIsValid: function (model) {
            for (var property in model) {
                if (model[property].hasError)
                    if (model[property].hasError())
                        return false;
            }
            return true;
        }
    };
})();